package com.jyu.sati.business.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jyu.sati.business.dao.AdminstratorDao;
import com.jyu.sati.business.dao.AuditInfoDao;
import com.jyu.sati.business.dao.ContactInfoDao;
import com.jyu.sati.business.dao.DocumentDao;
import com.jyu.sati.business.dao.PersonInfoDao;
import com.jyu.sati.business.dao.UserDao;
import com.jyu.sati.business.service.AdministratorService;
import com.jyu.sati.common.exception.BusinessException;
import com.jyu.sati.entity.Adminstrator;
import com.jyu.sati.entity.AuditInfo;
import com.jyu.sati.entity.ContactInfo;
import com.jyu.sati.entity.Document;
import com.jyu.sati.entity.Person;
import com.jyu.sati.entity.PersonInfo;
import com.jyu.sati.entity.User;
import com.jyu.sati.vo.AdministratorInfoVo;
import com.jyu.sati.vo.PersonInfoVo;
import com.jyu.sati.vo.UserInfoPageVo;
import com.jyu.sati.vo.UserInfoVo;

/**
 * 
 * 管理员服务
 * 
 * @author 淋雨又调皮
 *
 */
@Service
public class AdministratorServiceImpl extends BaseServiceImpl implements AdministratorService {

	@Autowired
	private AdminstratorDao adminDao;
	@Autowired
	private ContactInfoDao contactDao;
	@Autowired
	private DocumentDao documentDao;
	@Autowired
	private PersonInfoDao personInfoDao;
	@Autowired
	private UserDao userDao;

	@Transactional
	@Override
	public int deleteByAdminId(Integer administratorId) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.deleteByPrimaryKey(administratorId);
	}

	@Transactional
	@Override
	public int insert(Adminstrator adminstrator) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.insert(adminstrator);
	}

	@Transactional
	@Override
	public int insertSelective(Adminstrator adminstrator) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.insert(adminstrator);
	}

	@Transactional(readOnly = true)
	@Override
	public Adminstrator getAdminByAdminId(Integer administratorId) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.selectByPrimaryKey(administratorId);
	}

	@Transactional
	@Override
	public int updateSelective(Adminstrator adminstrator) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.updateByPrimaryKeySelective(adminstrator);
	}

	@Transactional
	@Override
	public int update(Adminstrator adminstrator) throws BusinessException {
		// TODO Auto-generated method stub
		return adminDao.updateByPrimaryKey(adminstrator);
	}

	@Transactional
	@Override
	public List<UserInfoVo> getSuperUserInfoPageByCondition(UserInfoPageVo condition) throws BusinessException {
		List<UserInfoVo> userInfos = null;
		try {
			condition.setTotalNo(getSuperTotalNoByCondition(condition));
			userInfos = adminDao.getSuperUserInfoPageByCondition(condition);
		} catch (Exception e) {
			errorLog("获取超级管理员信息失败！", e);
			throw new BusinessException(getMsg());
		}
		return userInfos;
	}

	@Transactional
	@Override
	public Integer getSuperTotalNoByCondition(UserInfoPageVo condition) throws BusinessException {
		Integer totalNo = 0;
		try {
			totalNo = adminDao.getSuperTotalNoByCondition(condition);
		} catch (Exception e) {
			errorLog("获取数据数目失败！", e);
			throw new BusinessException(getMsg());
		}
		return totalNo;
	}

	@Transactional
	@Override
	public List<UserInfoVo> getPlatformUserInfoPageByCondition(UserInfoPageVo condition) throws BusinessException {
		List<UserInfoVo> userInfos = null;
		try {
			condition.setTotalNo(getPlatformTotalNoByCondition(condition));
			userInfos = adminDao.getPlatformUserInfoPageByCondition(condition);
		} catch (Exception e) {
			errorLog("获取平台管理员信息失败！", e);
			throw new BusinessException(getMsg());
		}
		return userInfos;
	}

	@Transactional
	@Override
	public Integer getPlatformTotalNoByCondition(UserInfoPageVo condition) throws BusinessException {
		Integer totalNo = 0;
		try {
			totalNo = adminDao.getPlatformTotalNoByCondition(condition);
		} catch (Exception e) {
			errorLog("获取数据数目失败！", e);
			throw new BusinessException(getMsg());
		}
		return totalNo;
	}

	@Transactional
	@Override
	public AdministratorInfoVo getAdministratorInfo() throws BusinessException {
		// TODO Auto-generated method stub
		AdministratorInfoVo adminInfo = new AdministratorInfoVo();
		try {
			String userId = getCurrentUserId();
			Integer administratorId = null;
			User user = userDao.getUserByUserName(userId);
			Integer userType = user.getUserType();
			if (userType == null) {
				throw new BusinessException("无法判断用户类型");
			} else if (userType == User.USER_TYPE_PLATFORM) {
				administratorId = user.getAdministratorId();
			} else if (userType == User.USER_TYPE_SUPER) {
				administratorId = user.getSuperAdministratorId();
			} else {
				throw new BusinessException("用户类型异常！");
			}
			Adminstrator admin = adminDao.selectByPrimaryKey(administratorId);
			PersonInfo baseInfo = personInfoDao.selectByPrimaryKey(admin.getPbiId());// 个人基本信息
			ContactInfo contact = contactDao.getContactInfoByUserId(userId);// 联系人信息
			Document personImage = documentDao.selectByPrimaryKey(baseInfo.getPersonImageId());// 个人照片
			Document idCardImage = documentDao.selectByPrimaryKey(baseInfo.getIdcardImageId());// 身份证照片

			adminInfo = setAdministratorToVo(adminInfo, admin);
			adminInfo = setBaseInfoToVo(adminInfo, baseInfo);
			adminInfo = setContactInfoToVo(adminInfo, contact);
			adminInfo = setPeronImageToVo(adminInfo, personImage);
			adminInfo = setIdCardToVo(adminInfo, idCardImage);

		} catch (Exception e) {
			errorLog("获取管理员信息失败", e);
			throw new BusinessException(getMsg());
		}

		return adminInfo;
	}

	/**
	 * 绑定管理员信息
	 * 
	 * @param adminInfo
	 * @param baseInfo
	 * @return
	 */
	private AdministratorInfoVo setAdministratorToVo(AdministratorInfoVo adminInfo, Adminstrator admin) {
		if (admin != null) {
			adminInfo.setAdministratorId(admin.getAdministratorId());
			adminInfo.setLocationId(admin.getLocationId());
		}
		return adminInfo;
	}

	/**
	 * 绑定基本信息
	 * 
	 * @param adminInfo
	 * @param baseInfo
	 * @return
	 */
	private AdministratorInfoVo setBaseInfoToVo(AdministratorInfoVo adminInfo, PersonInfo baseInfo) {

		if (baseInfo != null) {
			adminInfo.setBaseInfoId(baseInfo.getPbiId());
			adminInfo.setPersonName(baseInfo.getPersonName());
			adminInfo.setSex(baseInfo.getSex());
			adminInfo.setNation(baseInfo.getNation());
			adminInfo.setBirth(baseInfo.getBirth());
			adminInfo.setEducationId(baseInfo.getEducationId());
			adminInfo.setWorkplace(baseInfo.getWorkplace());
			adminInfo.setBusiness(baseInfo.getBusiness());
			adminInfo.setPersonalProfile(baseInfo.getPersonalProfile());
			adminInfo.setRemark(baseInfo.getRemark());
			adminInfo.setIdcardNumber(baseInfo.getIdcardNumber());
		}
		return adminInfo;
	}

	/**
	 * 绑定个人图片
	 * 
	 * @param adminInfo
	 * @param personImage
	 * @return
	 */
	private AdministratorInfoVo setPeronImageToVo(AdministratorInfoVo adminInfo, Document personImage) {

		if (personImage != null) {
			adminInfo.setPersonImageId(personImage.getDocumentId());
			adminInfo.setPersonImageUrl(personImage.getDocumentUrl());
		}
		return adminInfo;
	}

	/**
	 * 绑定身份证信息
	 * 
	 * @param adminInfo
	 * @param idCardImage
	 * @return
	 */
	private AdministratorInfoVo setIdCardToVo(AdministratorInfoVo adminInfo, Document idCardImage) {
		if (idCardImage != null) {
			adminInfo.setIdcardImageId(idCardImage.getDocumentId());
			adminInfo.setImageUrl(idCardImage.getDocumentUrl());
		}
		return adminInfo;
	}

	/**
	 * @param adminInfo
	 * @param catactInfo
	 * @return
	 */
	private AdministratorInfoVo setContactInfoToVo(AdministratorInfoVo adminInfo, ContactInfo contactInfo) {
		if (contactInfo != null) {
			adminInfo.setContactId(contactInfo.getContactId());
			adminInfo.setContactAddress(contactInfo.getContactAddress());
			adminInfo.setContactBusiness(contactInfo.getContactBusiness());
			adminInfo.setContactName(contactInfo.getContactName());
			adminInfo.setPostcode(contactInfo.getPostcode());
			adminInfo.setEmail(contactInfo.getEmail());
			adminInfo.setContactUrl(contactInfo.getContactUrl());
			adminInfo.setFaxNumber(contactInfo.getFaxNumber());
			adminInfo.setQqormsnNumer(contactInfo.getQqormsnNumer());
			adminInfo.setPhoneNumber(contactInfo.getPhoneNumber());
			adminInfo.setContactNumber(contactInfo.getContactNumber());
		}

		return adminInfo;
	}

	@Transactional
	@Override
	public AdministratorInfoVo getAdministratorInfoByUserId(String userId) throws BusinessException {
		// TODO Auto-generated method stub
		AdministratorInfoVo adminInfo = new AdministratorInfoVo();
		try {
			Integer administratorId = null;
			User user = userDao.getUserByUserName(userId);
			Integer userType = user.getUserType();
			if (userType == null) {
				throw new BusinessException("无法判断用户类型");
			} else if (userType == User.USER_TYPE_PLATFORM) {
				administratorId = user.getAdministratorId();
			} else if (userType == User.USER_TYPE_SUPER) {
				administratorId = user.getSuperAdministratorId();
			} else {
				throw new BusinessException("用户类型异常！");
			}
			Adminstrator admin = adminDao.selectByPrimaryKey(administratorId);
			PersonInfo baseInfo = personInfoDao.selectByPrimaryKey(admin.getPbiId());// 个人基本信息
			ContactInfo contact = contactDao.getContactInfoByUserId(userId);// 联系人信息
			Document personImage = documentDao.selectByPrimaryKey(baseInfo.getPersonImageId());// 个人照片

			adminInfo = setAdministratorToVo(adminInfo, admin);
			adminInfo = setBaseInfoToVo(adminInfo, baseInfo);
			adminInfo = setContactInfoToVo(adminInfo, contact);
			adminInfo = setPeronImageToVo(adminInfo, personImage);

		} catch (Exception e) {
			errorLog("获取管理员信息失败", e);
			throw new BusinessException(getMsg());
		}

		return adminInfo;
	}

	@Transactional
	@Override
	public void updateAdministratorInfo(AdministratorInfoVo adminInfo) throws BusinessException {
		if (adminInfo == null) {
			errorLog("个人用户信息为空！");
			throw new BusinessException(getMsg());
		}
		try {
			updateAdministrator(adminInfo);
			updateBaseInfo(adminInfo);
			updateContact(adminInfo);
		} catch (Exception e) {
			errorLog("更新管理员信息失败！", adminInfo.toString(), e);
			throw new BusinessException(getMsg());
		}
	}

	/**
	 * 更新管理员信息
	 * 
	 * @param adminInfo
	 * @param baseInfo
	 * @return
	 */
	private void updateAdministrator(AdministratorInfoVo adminInfo) throws BusinessException {
		if (adminInfo == null) {
			throw new BusinessException("管理员信息为空！");
		}
		try {
			Adminstrator admin = new Adminstrator();
			admin.setAdministratorId(adminInfo.getAdministratorId());
			admin.setLocationId(adminInfo.getLocationId());
			adminDao.updateByPrimaryKeySelective(admin);
		} catch (Exception e) {
			errorLog("更新Administrator表失败", e);
			throw new BusinessException(getMsg());
		}

	}

	/**
	 * 更新基本信息
	 * 
	 * @param adminInfo
	 * @param baseInfo
	 * @return
	 */
	private void updateBaseInfo(AdministratorInfoVo adminInfo) throws BusinessException {

		PersonInfo baseInfo = new PersonInfo();
		baseInfo.setPbiId(adminInfo.getBaseInfoId());
		baseInfo.setPersonName(adminInfo.getPersonName());
		baseInfo.setSex(adminInfo.getSex());
		baseInfo.setNation(adminInfo.getNation());
		baseInfo.setBirth(adminInfo.getBirth());
		baseInfo.setEducationId(adminInfo.getEducationId());
		baseInfo.setWorkplace(adminInfo.getWorkplace());
		baseInfo.setBusiness(adminInfo.getBusiness());
		baseInfo.setPersonalProfile(adminInfo.getPersonalProfile());
		baseInfo.setRemark(adminInfo.getRemark());
		baseInfo.setIdcardNumber(adminInfo.getIdcardNumber());

		try {
			personInfoDao.updateByPrimaryKeySelective(baseInfo);
		} catch (Exception e) {
			errorLog("更新person表失败", e);
			throw new BusinessException(getMsg());
		}

	}

	/**
	 * 更新联系人
	 * 
	 * @param adminInfo
	 * 
	 * @param catactInfo
	 * @return
	 */
	private void updateContact(AdministratorInfoVo adminInfo) {

		ContactInfo contact = new ContactInfo();
		contact.setContactId(adminInfo.getContactId());
		contact.setContactAddress(adminInfo.getContactAddress());
		contact.setContactBusiness(adminInfo.getContactBusiness());
		contact.setContactName(adminInfo.getContactName());
		contact.setPostcode(adminInfo.getPostcode());
		contact.setEmail(adminInfo.getEmail());
		contact.setContactUrl(adminInfo.getContactUrl());
		contact.setFaxNumber(adminInfo.getFaxNumber());
		contact.setQqormsnNumer(adminInfo.getQqormsnNumer());
		contact.setPhoneNumber(adminInfo.getPhoneNumber());
		contact.setContactNumber(adminInfo.getContactNumber());

		try {
			contactDao.updateByPrimaryKeySelective(contact);
		} catch (Exception e) {
			errorLog("更新联系人信息失败！", e);
			throw new BusinessException(getMsg());
		}
	}

}
